package com.sojson.controller.po;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.AdParamVerify;
import com.sojson.entity.po.Ad;
import com.sojson.entity.vo.AdVo;
import com.sojson.enums.core.EGeneralStatus;
import com.sojson.result.Result;
import com.sojson.service.AdService;
import com.sojson.util.DateUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 广告表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "广告表管理")
@RestController
@RequestMapping(value = "/ad")
public class AdController {

    @Resource
    private AdService adServiceImpl;

    /**
     * 单条插入
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/ad/insert')")
    @ApiOperation(value = "添加广告", response = int.class)
    public Object insert(@RequestBody Ad ad) throws SQLException {
        // 校验参数
        Result<?> paramResult = AdParamVerify.insert(ad);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        ad.insertInit();
        adServiceImpl.insert(ad);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insertList", method = RequestMethod.POST)
    public Object insertList(@RequestBody Ad ad) throws SQLException {
        return Result.success(adServiceImpl.insertList(ad));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByDto", method = RequestMethod.POST)
    public Object deleteByDto(@RequestBody Ad ad) throws SQLException {
        return Result.success(adServiceImpl.deleteByDto(ad));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countAll", method = RequestMethod.GET)
    public Object countAll() throws SQLException {
        int countAll = adServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() throws SQLException {
        List<Ad> findAll = adServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countByDto", method = RequestMethod.POST)
    public Object countByDto(@RequestBody Ad ad) throws SQLException {
        int countByDto = adServiceImpl.countByDto(ad);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @GetMapping("/findByDto")
    @PreAuthorize("@ss.hasPermi('/ad/findByDto')")
    @ApiOperation(value = "根据对象查询广告", response = Ad.class)
    public Object findByDto(Ad ad) throws SQLException {
        List<Ad> findByDto = adServiceImpl.findByDto(ad);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @DeleteMapping(value = "/deleteById")
    public Object deleteById(Integer id) throws SQLException {
        adServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @DeleteMapping(value = "/deleteByIdList")
    @PreAuthorize("@ss.hasPermi('/ad/deleteByIdList')")
    @ApiOperation(value = "通过id列表删除广告(物理删除)", response = int.class)
    public Object deleteByIdList(@RequestParam List<Integer> ids) throws SQLException {
        if (ids.size() < 1) {
            return Result.success();
        }
        return Result.success(adServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Object updateById(@RequestBody Ad ad) throws SQLException {
        adServiceImpl.updateById(ad);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticByIdList", method = RequestMethod.POST)
    public Object updateStaticByIdList(@RequestBody Ad ad) throws SQLException {
        return Result.success(adServiceImpl.updateStaticByIdList(ad));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangeByIdList", method = RequestMethod.POST)
    public Object updateChangeByIdList(@RequestBody Ad ad) throws SQLException {
        return Result.success(adServiceImpl.updateChangeByIdList(ad));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @PutMapping("/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/ad/updatePartialById')")
    @ApiOperation(value = "通过id更新广告(局部更新)", response = int.class)
    public Object updatePartialById(@RequestBody Ad ad) throws SQLException {
        // 校验参数
        Result<?> paramResult = AdParamVerify.updatePartialById(ad);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        ad.updateInit();
        adServiceImpl.updatePartialById(ad);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticPartialByIdList", method = RequestMethod.POST)
    public Object updateStaticPartialByIdList(@RequestBody Ad ad) throws SQLException {
        return Result.success(adServiceImpl.updateStaticPartialByIdList(ad));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangePartialByIdList", method = RequestMethod.POST)
    public Object updateChangePartialByIdList(@RequestBody Ad ad) throws SQLException {
        return Result.success(adServiceImpl.updateChangePartialByIdList(ad));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ApiOperation(value = "通过id查询广告", response = Ad.class)
    public Object findById(Integer id) throws SQLException {
        Ad findById = adServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByIdList", method = RequestMethod.GET)
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<Ad> findByIdList = adServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findIdByDto", method = RequestMethod.POST)
    public Object findIdByDto(@RequestBody Ad ad) throws SQLException {
        ArrayList<Integer> findIdByDto = adServiceImpl.findIdByDto(ad);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Object clear() throws SQLException {
        adServiceImpl.clear();
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param ad
     * @return
     * @throws SQLException
     */
    @GetMapping("/findAppByDto")
    @ApiOperation(value = "根据对象查询广告", response = Ad.class)
    public Object findAppByDto(AdVo ad) throws SQLException {
        ad.setStatus(EGeneralStatus.NORMAL.getCode());
        ad.setThisTime(DateUtil.nowDate());
        List<Ad> findAppByDto = adServiceImpl.findAppByDto(ad);
        return Result.success(findAppByDto, findAppByDto);
    }

}